package start.designpattern.composite;

import lombok.extern.slf4j.Slf4j;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by jingbin on 2020-02-02.
 * 1. 创建 Employee 类，该类带有 Employee 对象的列表。
 *
 * /**
 *  * @author jingbin
 *  * 组合模式（Composite Pattern），又叫部分整体模式，是用于把一组相似的对象当作一个单一的对象。
 *  * 组合模式依据树形结构来组合对象，用来表示部分以及整体层次。
 *  * 这种类型的设计模式属于结构型模式，它创建了对象组的树形结构。
 *  * <p>
 *  * 主要解决：
 *  * 它在我们树型结构的问题中，模糊了简单元素和复杂元素的概念，
 *  * 客户程序可以像处理简单元素一样来处理复杂元素，从而使得客户程序与复杂元素的内部结构解耦。
 *
 *  */

@Slf4j
public class Employee {

    private String name;
    // 部门
    private String dept;
    // 工资
    private int salary;
    // 员工 list
    private List<Employee> subordinates;

    public Employee(String name, String dept, int salary) {
        this.name = name;
        this.dept = dept;
        this.salary = salary;
        this.subordinates = new ArrayList<Employee>();
    }

    public void add(Employee e) {
        subordinates.add(e);
    }

    public void remove(Employee e) {
        subordinates.remove(e);
    }

    public List<Employee> getSubordinates() {
        return subordinates;
    }

    @Override
    public String toString() {
        return "Employee{" +
                "name='" + name + '\'' +
                ", dept='" + dept + '\'' +
                ", salary=" + salary +
                ", subordinates=" + subordinates +
                '}';
    }

    public static void main(String[] args) {
        final Employee ceo = new Employee("John", "CEO", 30000);

        Employee headSales = new Employee("Robert", "Head sales", 20000);

        Employee headMarketing = new Employee("Michel", "Head Marketing", 20000);

        Employee clerk1 = new Employee("Laura", "Marketing", 10000);
        Employee clerk2 = new Employee("Bob", "Marketing", 10000);

        Employee salesExecutive1 = new Employee("Richard", "Sales", 10000);
        Employee salesExecutive2 = new Employee("Rob", "Sales", 10000);

        ceo.add(headSales);
        ceo.add(headMarketing);

        headSales.add(salesExecutive1);
        headSales.add(salesExecutive2);

        headMarketing.add(clerk1);
        headMarketing.add(clerk2);

        //打印该组织的所有员工
        System.out.println("CEO>>>\n" + ceo);
        for (Employee headEmployee : ceo.getSubordinates()) {
            System.out.println("CEO.Subordinates>>>\n" + headEmployee);
            for (Employee employee : headEmployee.getSubordinates()) {
                System.out.println("CEO.Subordinates.Subordinates>>>\n" + employee);
            }
        }

        // 打印
        /*
         * Employee{name='John', dept='CEO', salary=30000,
         * subordinates=[Employee{name='Robert', dept='Head sales', salary=20000,
         * subordinates=[
         * Employee{name='Richard', dept='Sales', salary=10000, subordinates=[]},
         * Employee{name='Rob', dept='Sales', salary=10000, subordinates=[]}]},
         * Employee{name='Michel', dept='Head Marketing', salary=20000,
         * subordinates=[Employee{name='Laura', dept='Marketing', salary=10000, subordinates=[]},
         * Employee{name='Bob', dept='Marketing', salary=10000, subordinates=[]}]}]}
         */
    }
}
